package cn.ailanyin.home.listen;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * CanalClient
 *
 * @author natalie
 * @date 2024/5/7
 */
@Component
@RequiredArgsConstructor
public class CanalDataListen implements InitializingBean {

    private final String redisKey = "myHome:";

    private final RedisTemplate<String, Object> redisTemplate;

    private CanalConnector connector;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 初始化连接
        connector = CanalConnectors.newSingleConnector(new InetSocketAddress("127.0.0.1", 11111), "example", "", "");
        // 开始连接
        connector.connect();
        // 订阅数据库的表
        connector.subscribe(".*\\..*");

    }


}
